package com.tz.farmer.controller;

import cn.hutool.core.util.StrUtil;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.model.CheckApply;
import com.tz.farmer.model.CheckRecords;
import com.tz.farmer.service.CheckApplyService;
import com.tz.farmer.service.CheckRecordsService;
import com.tz.farmer.util.OssUtils;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @description: 检测申请（h5）
 * @name BusinessController
 * @author: xhj
 * @date: 2021/6/23
 * @version V1.0
 */
@RestController
@RequestMapping("check")
@Api(value = "检测申请（h5）", tags = "检测申请（h5）控制器")
public class CheckApplyController {
    private static Logger logger = LoggerFactory.getLogger(CheckApplyController.class);

    @Resource
    CheckApplyService checkApplyService;

    @Resource
    CheckRecordsService checkRecordsService;
    /**
     * @description: 检测申请提交
     * @name submitCheckApply
     * @author: xhj
     * @date: 2021/7/15 11:35
     * @version V1.0
     */
    @PostMapping("/submitCheckApply")
    public ResultEx submitCheckApply(@RequestParam("photos") MultipartFile[] photos, @RequestParam String checkType,@RequestParam String checkContent
            ,@RequestParam String name,@RequestParam String sex,@RequestParam String phone,@RequestParam String flag,@RequestParam String checkName,@RequestParam String goodsId) {
        String photoUrl="";
        try {
            if(photos!=null&&photos.length>0){
                for (MultipartFile photo : photos) {
                    String fileName = photo.getOriginalFilename();
                    //文件上传到oss，返回文件路径
                    String uploadUrl = OssUtils.uploadOssFile(photo, "file/" + fileName);
                    logger.info("oss图片路径=============="+uploadUrl);
                    //多个文件逗号隔开
                    photoUrl = StrUtil.join(",", photoUrl, uploadUrl);
                    logger.info("文件上传OK");

                }
                photoUrl=photoUrl.substring(1,photoUrl.length());
            }
            checkApplyService.save(CheckApply.builder().checkType(checkType).checkContent(checkContent).name(name).sex(sex)
                    .phone(phone).property(Integer.valueOf(flag)).photo(photoUrl).uploadTime(LocalDateTime.now()).checkName(checkName).goodsId(goodsId).build());

            CheckRecords checkRecords=CheckRecords.builder().property(Integer.valueOf(flag)).checkTime(LocalDateTime.now()).build();
            checkRecordsService.save(checkRecords);

        }catch (Exception e){
            logger.error("提交失败",e);
            return ResultEx.builder().makeFailedResult("提交失败");

        }
        return ResultEx.builder().makeSuccessResult("提交成功");

    }

}
